package com.zut.controller;

import com.aliyuncs.exceptions.ClientException;
import com.zut.constant.MessageConstant;
import com.zut.constant.RedisMessageConstant;
import com.zut.entity.Result;
import com.zut.utils.SMSUtils;
import com.zut.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * 发送短信验证码
 * @author sq
 * @create 2021-07-14-16:39
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;
    //用户在线体检预约发送验证码
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone) {
        //随机生成4位数字的验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        try {
            //通过阿里云的短信服务发送短信
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, validateCode.toString());
        } catch (Exception e) {
            e.printStackTrace();
            //发送失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将验证码保存在redis中（有效时间为5分钟）
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 300, validateCode.toString());
        //发送成功
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,validateCode);
    }

    //用户收集快速登录发送验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone) {
        //随机生成6位数字的验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        try {
            //通过阿里云的短信服务发送短信
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, validateCode.toString());
        } catch (Exception e) {
            e.printStackTrace();
            //发送失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将验证码保存在redis中（有效时间为5分钟）
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN, 300, validateCode.toString());
        //发送成功
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,validateCode);
    }
}
